package com.wswy.wzcx.ui.module.feedback

import android.arch.lifecycle.Observer
import android.content.Context
import android.os.Bundle
import android.support.transition.TransitionManager
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.che.libcommon.ui.adapter.SampleRecyclerViewAdapter
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R
import com.wswy.wzcx.model.home.RPaths
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.module.LocManager
import com.wswy.wzcx.module.base.*
import com.wswy.wzcx.utils.ResUtils
import kotlinx.android.synthetic.main.fragment_feedback.*

class FeedbackFragment : CBaseFragment() {


    override fun getFragmentTitle(): String {
        return "帮助与反馈"
    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_feedback
    }

    override fun getTitleRightView(context: Context): View? {
        return  TextView(context).apply {

            val dp16 = SizeUtils.dp2px(16f)

            textSize = 14f
            setTextColor(ContextCompat.getColor(context,R.color.colorPrimary))
            setPadding(dp16,0,dp16,0)

            text = "反馈记录"
            gravity = Gravity.CENTER
            includeFontPadding = false

            setBackgroundResource(ResUtils.getSelectableItemBackground(context))
            isClickable = true

            setOnClickListener(myFeedbackClickListener)
            layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }


    private val myFeedbackClickListener = View.OnClickListener {

        if(feedbackViewModel.hasLogin()){
            RouterUtils.start(context,RPaths.MY_FEEDBACK)
        }else{
            feedbackViewModel.gotoLogin(it.context)
        }


    }

    private lateinit var feedbackViewModel:FeedbackViewModel

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val context = requireContext()

        feedbackViewModel = provideViewModel(FeedbackViewModel::class.java)
        feedbackViewModel.commonFeedback()


        tv_want_feedback.setOnClickListener {
            if (feedbackViewModel.hasLogin()){
                RouterUtils.start(context,RPaths.ADD_FEEDBACK)
            }else{
                feedbackViewModel.gotoLogin(it.context)
            }

        }


        recycler_view.layoutManager = LinearLayoutManager(context)

        val dp10 = SizeUtils.dp2px(10f)

        recycler_view.addOffset(dp10,dp10,dp10)

        val adapter= FeedbackAdapter()

        recycler_view.adapter = adapter


        feedbackViewModel.getCommonFeedbackLiveData().observe(this, Observer {

            it?.run {

                if (isOk()){

                    adapter.setNewData(data)
                    adapter.notifyDataSetChanged()

                }

            }

        })
    }


   private class FeedbackAdapter : SampleRecyclerViewAdapter<CommonFeedbackQA,FeedbackViewHolder>(){

       private val expandSet = hashSetOf<Int>()
       private val expandColor = ContextCompat.getColor(AppContext.getContext(),R.color.colorPrimary)
       private val defaultColor = ContextCompat.getColor(AppContext.getContext(),R.color.textColorPrimary)

       init {
           setOnItemClickListener { _, position, _ ->

               if (expandSet.contains(position)){
                   expandSet.remove(position)
               }else{
                   expandSet.add(position)
               }
               notifyItemChanged(position)

           }
       }

       override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedbackViewHolder {
           return FeedbackViewHolder(loadLayout(parent,R.layout.item_feedback_common_qa))
       }

       override fun onBindViewHolder(holder: FeedbackViewHolder, position: Int, data: CommonFeedbackQA?) {
           val isExpanded = expandSet.contains(position)

           if (isExpanded){
               holder.tvQuestion.setTextColor(expandColor)
               holder.tvAnswer.setTextColor(expandColor)

               holder.llSubAnswer.visible()
               holder.imgRight.rotation = 90f
           }else{

               holder.tvQuestion.setTextColor(defaultColor)
               holder.tvAnswer.setTextColor(defaultColor)

               holder.llSubAnswer.gone()
               holder.imgRight.rotation = 0f
           }


           holder.tvQuestion.text = "${position+1}.${data?.question}"
           holder.tvAnswer.text = data?.answer

           holder.itemView.isActivated = isExpanded
       }

       override fun setNewData(list: List<CommonFeedbackQA>?) {
           super.setNewData(list)
           expandSet.clear()
       }

   }
    private class FeedbackViewHolder (itemView:View): RecyclerView.ViewHolder(itemView){

        val tvQuestion:TextView = itemView.findViewById(R.id.tv_question)
        val imgRight:ImageView = itemView.findViewById(R.id.img_right)
        val llSubAnswer:View = itemView.findViewById(R.id.ll_sub_answer)

        val tvAnswer : TextView = itemView.findViewById(R.id.tv_answer)

    }

}